package com.smartappsnetwork.personalexpenses.dataaccess;

import java.util.Vector;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import com.smartappsnetwork.personalexpenses.LauncherApplication;
import com.smartappsnetwork.personalexpenses.database.DatabaseHelper;
import com.smartappsnetwork.personalexpenses.dataobject.CategoryDO;
import com.smartappsnetwork.personalexpenses.dataobject.PC_GeneralSettingsDO;
import com.smartappsnetwork.personalexpenses.dataobject.PayModeDO;

public class PayModeDA 
{

	public long insertPaymode(PayModeDO payModeDO)
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 
			 String query = "SELECT * FROM paymode WHERE name=?";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, new String[]{""+payModeDO.name});
					 if(cursor!=null && cursor.moveToNext())
					 {
//						 updateGeneralSettings(generalSettingsDO);
					 }
					 else
					 {
						 SQLiteStatement stmt    = database.compileStatement("INSERT INTO paymode (name, access) VALUES (?,?)");
						 stmt.bindString(1, payModeDO.name);
						 stmt.bindLong(2, payModeDO.access);
						 appID = stmt.executeInsert();
						 stmt.close();
					 }
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("paymode", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return appID;
		}
	}
	
	public long  updatePayMode(PayModeDO payModeDO)
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
						
					 //1- true , 0 -false
					 SQLiteStatement stmt = database.compileStatement("update  paymode set name= '"+payModeDO.name+"', access = access+1 where cid="+payModeDO.pid);
					 
					 appID = stmt.executeInsert();
					 stmt.close();
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("update", "paymode : "+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return appID;
		}
	}
	
	
	public PayModeDO getPayModeById(int pid)
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 PayModeDO payModeDO = null;
			 String query = "SELECT * FROM paymode WHERE pid=?";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, new String[]{""+pid});
					 if(cursor!=null && cursor.moveToFirst())
					 {
						 do
						 {
							 payModeDO = new PayModeDO();
							 payModeDO.pid = cursor.getLong(0);
							 payModeDO.name = cursor.getString(1);
							 payModeDO.access = cursor.getLong(2);
							 
						 }while(cursor.moveToNext());
					 }
					
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("paymode", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return payModeDO;
		}
		
	}
	
	
	public String getPayModeNameById(int pid)
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 String name = null;
			 String query = "SELECT name FROM paymode WHERE pid=?";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, new String[]{""+pid});
					 if(cursor!=null && cursor.moveToFirst())
					 {
						 do
						 {
							 name = cursor.getString(0);
							 
						 }while(cursor.moveToNext());
					 }
					
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("paymode", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return name;
		}
		
	}
	
	
	
	

	public Vector<PayModeDO> getAllPayModes()
	{
		synchronized (LauncherApplication.databaseLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 Vector<PayModeDO> vecModeDO = new Vector<PayModeDO>();;
			 String query = "SELECT * FROM paymode";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, null);
					 if(cursor!=null && cursor.moveToFirst())
					 {
						 do
						 {
							 PayModeDO payModeDO = new PayModeDO();
							 payModeDO.pid = cursor.getLong(0);
							 payModeDO.name = cursor.getString(1);
							 payModeDO.access = cursor.getLong(2);
							 vecModeDO.add(payModeDO);
							 
						 }while(cursor.moveToNext());
					 }
					
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("tblParentalCntrolGeneralSettings", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return vecModeDO;
		}
	
		
	}
	

	public void deletePayModeById(int pid)
	{
		synchronized (LauncherApplication.databaseLock) 
		{
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
	
					 String whereClause = "cid=?";
					 database.delete("paymode", whereClause, new String[]{""+pid});
				 }
			 }
			 catch(Exception e)
			 {
				 e.printStackTrace();
			 }
			 finally
			 {
				 
				 if(database!=null && database.isOpen())
					 database.close();
			 }
		}
		 
	}
	
	
	public void deletePayModeByName(String name)
	{
		synchronized (LauncherApplication.databaseLock) 
		{
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
	
					 String whereClause = "name=?";
					 database.delete("paymode", whereClause, new String[]{""+name});
				 }
			 }
			 catch(Exception e)
			 {
				 e.printStackTrace();
			 }
			 finally
			 {
				 
				 if(database!=null && database.isOpen())
					 database.close();
			 }
		}
		 
	}


}
